//
//  UIView+cycleNoDataView.m
//  CycleControlFunction
//
//  Created by ddy on 2018/10/8.
//  Copyright © 2018年 cycle. All rights reserved.
//

#import "UIView+cycleNoDataView.h"
#import "UIView+CycleFrame.h"
#define noDataViewTag 1212122222
@implementation UIView (cycleNoDataView)
///1.显示无数据的内容
- (void)cycle_showNoDataViewWithContent:(NSString *)content {
    //移除之前的
    [self cycle_dismissNoDataView];
    UIView *view = [self noDataViewWithContent:content];
    view.center = CGPointMake(self.c_width/2, self.c_height/2+10);
    [self addSubview:view];
}
///暂无数据方法
- (UIView *)noDataViewWithContent:(NSString *)content {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    view.tag = noDataViewTag;
    //无数据图片
    UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    imgView.frame = CGRectMake(0, 0, 150, 120);
    imgView.center = CGPointMake(view.center.x, view.center.y-30);
    [view addSubview:imgView];
    
    //无数据文字
    UILabel *label = [UILabel new];
    label.text = content;
    label.font = [UIFont systemFontOfSize:14];
    label.frame = CGRectMake(0, imgView.c_top+15, view.c_width, 15);
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    return view;
    
}
///退出暂无数据View
- (void)cycle_dismissNoDataView
{
    for (UIView *view in self.subviews)
    {
        if (view.tag == noDataViewTag) {
            [view removeFromSuperview];
        }
    }
}
///2.1 显示无数据的内容和图片
- (void)cycle_showNoDataViewWithContent:(NSString *)content andImage:(NSString *)imageName{
    //先移除之前的数据
    [self cycle_dismissNoDataView];
    
    UIView *addView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    addView.tag = noDataViewTag;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [addView addSubview:imageView];
    imageView.frame = CGRectMake(0, 0, 150, 120);
    imageView.center = CGPointMake(addView.center.x, addView.center.y - 30);
    //标题
    UILabel *label = [UILabel new];
    label.text = content;
    label.font = [UIFont systemFontOfSize:14];
    label.frame = CGRectMake(0, imageView.c_top+15, addView.c_width, 15);
    label.textAlignment = NSTextAlignmentCenter;
    [addView addSubview:label];
    
    addView.center = CGPointMake(self.c_width/2, self.c_height/2+10) ;
    [self addSubview:addView];
}
//2.2 显示无数据的内容和图片,按钮标题以及点击按钮的操作
- (void)smc_showNoDataViewWithContent:(NSString *)content andImage:(NSString *)imageName buttonTitle:(NSString *)btnTitle block:(void(^)(void))block{
    //先移除之前的数据
    [self cycle_dismissNoDataView];
    
    CGFloat btnW = 117;
    CGFloat btnH = 37;
    CGFloat btnX = 0;
    CGFloat btnContainerTotalHeight = 20+btnH+30;
    //addContainerView
    UIView *addView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150+btnContainerTotalHeight)];
    addView.tag = noDataViewTag;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [addView addSubview:imageView];
    imageView.frame = CGRectMake(0, 0, 150, 120);
    imageView.center = CGPointMake(addView.center.x, addView.center.y - 30);
    UILabel *label = [self createGrayLabelText:content font:[UIFont systemFontOfSize:14.f]];
    label.frame = CGRectMake(0, imageView.c_top+15, addView.c_width, 15);
    label.textAlignment = NSTextAlignmentCenter;
    [addView addSubview:label];
    //按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:btnTitle forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:238/255.0 green:155/255.0 blue:95/255.0 alpha:1] forState:UIControlStateNormal];
    btnX = (addView.c_width-btnW)*0.5;
    button.frame = CGRectMake(btnX, label.c_top+20, btnW, btnH);
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    button.layer.cornerRadius = 20;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = [UIColor colorWithRed:238/255.0 green:155/255.0 blue:95/255.0 alpha:1].CGColor;
    button.layer.borderWidth = 1;
    [addView addSubview:button];
    //点击 自行处理
//
//    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        if (block) {
//            block();
//        }
//    }];
    //x,y
    addView.center = CGPointMake(self.c_width/2, 120) ;
    [self addSubview:addView];
}
- (UILabel *)createGrayLabelText:(NSString *)t font:(UIFont *)f
{
    UILabel *label = [UILabel new];
    label.font = f;
    label.text = t;
    label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
    return label;

}

//3.显示无数据的内容和图片(可计算frame)
- (void)cycle_showNoDataViewWithFrame:(CGRect)frame Content:(NSString *)content andImage:(NSString *)imageName{
    //先移除之前的数据
    [self cycle_dismissNoDataView];
    
    UIView *addView = [[UIView alloc]initWithFrame:frame];
    addView.tag = noDataViewTag;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    [addView addSubview:imageView];
    imageView.frame = CGRectMake(0, 0, 150, 120);
    imageView.center = CGPointMake(addView.center.x, addView.center.y - 30);
    UILabel *label = [UILabel new];
    label.text = content;
    label.font = [UIFont systemFontOfSize:14];
    label.frame = CGRectMake(0, imageView.c_top+15, addView.c_width, 15);
    label.textAlignment = NSTextAlignmentCenter;
    [addView addSubview:label];
    
    addView.center = CGPointMake(self.c_width/2, self.c_height/2+10) ;
    [self addSubview:addView];
}


@end
